package zut.edu.cs.network.practice16;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import zut.edu.cs.network.base.BaseMainCase;

public class Method extends BaseMainCase {
	/*实验16 题1*/
	public void searchfile(String path) throws IOException {
		InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
		// InputStreamReader将字符流向字节流转换。
		// InputStreamReader isr = new InputStreamReader(new FileInputStream(绝对文件名));
		// 用来读取文件中的数据
		BufferedReader br = new BufferedReader(isr);// 使用缓冲区，可以使用缓冲区的read(),readLine()方法；
		/*
		 * readLine每次读取一行，read()读取整个文件，是生成文件内容最直接的方式，如果连续面向行的处理则是没有必要的 可直接综合为
		 * BufferedReader br = new BufferedReader(new InputStreamReader(new
		 * FileInputStream(path)));
		 */
		HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
		hashMap.put("characters", 0);
		hashMap.put("word", 0);
		hashMap.put("line", 0);
		while (br.read() != -1)// read()=-1代表数据读取完毕
		{
			String s = br.readLine();
			System.out.println(hashMap.get("characters") + s.length());
			hashMap.put("characters", hashMap.get("characters") + s.length());// 字符个数就是字符长度
			hashMap.put("word", hashMap.get("word") + s.split(" ").length);// split() 方法用于把一个字符串分割成字符串数组,字符串数组的长度，就是单词个数
			hashMap.put("line", hashMap.get("line") + 1);// 因为是按行读取，所以每次增加一即可计算出行的数目
		}
		isr.close();// 关闭文件
		logger.info("characters:" + hashMap.get("characters"));
		logger.info("word:" + hashMap.get("word"));
		logger.info("line:" + hashMap.get("line"));
	}
	/*实验16 题2*/
	public void deleteString(String s, String filepath) throws IOException {
		File file = new File(filepath);
		BufferedReader reader = new BufferedReader(new FileReader(file));
		StringBuffer strs = new StringBuffer();
		String line = null;
		while ((line = reader.readLine()) != null)// read()=-1代表数据读取完毕
		{
			strs.append(line + "\n");
		}
		reader.close();
		String string = strs.toString();
		Pattern pattern = Pattern.compile(s);
		Matcher matcher = pattern.matcher(string);
		string = matcher.replaceAll("");
		logger.info(strs);
		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
		writer.write(string);
		writer.flush();
		writer.close();
	}
	/*实验16 题3*/
	public void writeToFile(String file) throws IOException {
		int num = 0;
		File target = new File(file);
		if (target.exists() && target.isFile()) {
			boolean flag = target.delete();
		}
		if (target.createNewFile()) {//写入数据
			for (int i = 1; i <= 100; i++) {
				FileWriter fileWriter = new FileWriter(target.getAbsoluteFile(), true);
				fileWriter.write(i);
				fileWriter.close();
			}
		}
		@SuppressWarnings("resource")
		FileReader fileReader = new FileReader(target.getAbsoluteFile());
		for (int i = 0; i < 100; i++) {
			num += fileReader.read();
		}
		logger.info("1-100和为:" + num);
	}
	/*实验16 题4*/
	public void motifyAddressBook() {

	}
}
